/*
 * Copyright (C) ${year} Jeffrey Palm.
 *
 * This code is part of the Floto Project (http://jeffpalm.com/floto/).
 * Please consult the LICENSE included in the distribution.
 */
package com.jeffpalm.floto.isight.osx;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;

import javax.imageio.ImageIO;

import com.jeffpalm.floto.isight.AbstractCapture;
import com.jeffpalm.floto.isight.GuiUtil;

/**
 * Captures a screen shot to a file.
 */
public class MacScreenCapture extends AbstractCapture.ScreenShot {

  public Snapshot takeSnapshot() throws Exception {
    String outFileName = System.currentTimeMillis() + ".jpg";
    File outFile = takeScreenShot(new File(outFileName));
    BufferedImage image = ImageIO.read(new FileInputStream(outFile));
    outFile.delete();
    return new SnapshotImpl(image);
  }

  public File takeScreenShot(final File outFile) throws Exception {
    new AppleScriptExecutor().execute("do shell script \"screencapture -tjpg \" & quoted form of \""
        + outFile.getName() + "\"");
    return outFile;
  }

  public static void main(String[] args) throws Exception {
    Snapshot ss = new MacScreenCapture().takeSnapshot();
    GuiUtil.openImageFrame(ss.getImage(), true);
  }
}
